package com.tieshan.smartwindow.ui

import android.view.View
import com.tencent.mmkv.MMKV
import com.tieshan.smartwindow.R
import com.tieshan.smartwindow.app.AppActivity
import com.tieshan.smartwindow.other.FileConfig
import com.tieshan.smartwindow.other.FileUtil
import com.tieshan.smartwindow.other.MMKVConfig
import kotlinx.android.synthetic.main.activity_usb.*

class USBActivity : AppActivity() {
    private val USBSetKV: MMKV = MMKV.mmkvWithID(MMKVConfig.USBSet)

    override fun getLayoutId(): Int {
        return R.layout.activity_usb
    }

    override fun initView() {
        tv_usb.setOnClickListener {
            var usb = USBSetKV.getBoolean(MMKVConfig.USBSet, false)
            usb = !usb
            USBSetKV.putBoolean(MMKVConfig.USBSet, usb)
            if (usb) {
                tv_usb.setTextColor(this.resources.getColor(R.color.red))
                tv_usb.text = "USB监听已打开"
                ll_usb.visibility=View.VISIBLE
            }else {
                tv_usb.setTextColor(this.resources.getColor(R.color.white))
                tv_usb.text = "USB监听已关闭"
                FileUtil.delAllFile(FileConfig.TouchLogPath)
                ll_usb.visibility=View.GONE
            }
        }
        bt_close.setOnClickListener {
            var time=et_close.text.toString().toInt()
            MMKV.mmkvWithID(MMKVConfig.USBcloseTime).putInt(MMKVConfig.USBcloseTime,time)
        }
        bt_open.setOnClickListener {
            var time=et_open.text.toString().toInt()
            MMKV.mmkvWithID(MMKVConfig.USBopenTime).putInt(MMKVConfig.USBopenTime,time)
        }
        iv_home.setOnClickListener {
            finish()
        }
    }

    override fun initData() {
        var usb = USBSetKV.getBoolean(MMKVConfig.USBSet, false)
        if (usb) {
            tv_usb.setTextColor(this.resources.getColor(R.color.red))
            tv_usb.text = "USB监听已打开"
            ll_usb.visibility=View.VISIBLE
        }else {
            tv_usb.setTextColor(this.resources.getColor(R.color.white))
            tv_usb.text = "USB监听已关闭"
            FileUtil.delAllFile(FileConfig.TouchLogPath)
            ll_usb.visibility=View.GONE
        }
        et_open.setText("${MMKV.mmkvWithID(MMKVConfig.USBopenTime).getInt(MMKVConfig.USBopenTime,2)}")
        et_close.setText("${MMKV.mmkvWithID(MMKVConfig.USBcloseTime).getInt(MMKVConfig.USBcloseTime,600)}")
    }
}